package com.kitty.game.fight.factory;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.activity.service.task.TowerTaskHandler;
import com.kitty.game.config.NPC;
import com.kitty.game.config.TaskSet;
import com.kitty.game.config.XinJun;
import com.kitty.game.fight.bean.Fight;
import com.kitty.game.fight.bean.FightObject;
import com.kitty.game.fight.bean.TowerFightObject;
import com.kitty.game.fight.factory.model.TowerFight;
import com.kitty.game.fight.message.vo.FightMember;
import com.kitty.game.fight.service.FightMessageService;
import com.kitty.game.rank.model.record.TowerMirrorRankRecord;
import com.kitty.game.role.model.Role;
import com.kitty.game.task.model.product.entry.FightSide;
import com.kitty.game.team.model.Member;
import com.kitty.game.team.model.Team;
import com.kitty.game.team.service.TeamService;
import com.kitty.mina.message.Message;

import java.text.MessageFormat;
import java.util.concurrent.ThreadLocalRandom;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TowerFightFactory extends FightFactory {

    private static final List<String> SMALL_XIN_JUN_NAMES = Arrays.asList("疆良", "玄武", "朱雀", "东山神灵");

    private static final String FIGHT_START_TIP = "#R{0}#n在此，汝须用心参悟！";

    @Override
    public Fight create(int mapId, Object... params) {
        Role role = (Role) params[0];
        TaskSet taskSet = (TaskSet) params[1];
        NPC bossNpc = (NPC) params[2];

        FightSide fightSide = newFightSide(role, bossNpc);
        return new TowerFight(role, taskSet, fightSide);
    }

    private FightSide newFightSide(Role role, NPC bossNpc) {
        FightSide fightSide = new FightSide();

        List<Role> roles = null;
        TeamService teamService = SpringUtils.getTeamService();
        if (teamService.isInTeam(role)) {
            Team team = teamService.getTeam(role.getRoleId());
            roles = new ArrayList<>(team.getList().size());

            for (Member member : team.getList()) {
                if (!member.isInTeam()) { continue; }
                Role memberRole = SpringUtils.getRoleService().getOnlinePlayer(member.getUid());
                if (memberRole == null) {continue;}

                roles.add(memberRole);
            }
        } else {
            roles = new ArrayList<>(1);
            roles.add(role);
        }

        int index = 0;
        int monsterId = 564857878;
        Map<Integer, Integer> hostIds = new HashMap<>(roles.size());
        for (Role tempRole : roles) {
            /**突破前加载队长星君进入战斗，突破后加载各成员星君进入战斗*/
            Role fightDataRole = tempRole.getActivity().getRoleTowerData().isInBreakStage() ? tempRole : role;
            setTowerToFightSide(fightSide, index, monsterId, fightDataRole);
            hostIds.put(tempRole.getRoleId(), monsterId);

            index++;
            monsterId++;
        }

        index = 5;
        for (Role tempRole : roles) {
            Integer hostId = hostIds.get(tempRole.getRoleId());
            /**突破前加载队长星君进入战斗，突破后加载各成员星君进入战斗*/
            Role fightDataRole = tempRole.getActivity().getRoleTowerData().isInBreakStage() ? tempRole : role;
            setTowerPetToFightSide(fightSide, index, monsterId, fightDataRole, hostId);

            index++;
            monsterId++;
        }

        // 写入野外战斗怪物给的经验
        fightSide.setExp(9999);
        Message fightMsg = SpringUtils.getBean(FightMessageService.class).fightMsg(fightSide.getFightObjectList().get(0), MessageFormat.format(FIGHT_START_TIP, bossNpc.getName()));
        fightSide.setFightMsg(fightMsg);
        return fightSide;
    }

    private void setTowerToFightSide(FightSide fightSide, int index, int monsterId, Role role) {
        String name = role.getActivity().getRoleTowerData().getCurrentXinJun();
        int layer = role.getActivity().getRoleTowerData().getCurrentLayer();
        XinJun xinJun = SpringUtils.getBean(TowerTaskHandler.class).getXinjun(name);
        NPC npc = SpringUtils.getMapService().getNpcByName(xinJun.getName());
        TowerMirrorRankRecord towerRankRecord = SpringUtils.getBean(TowerTaskHandler.class).getXinJunRankData(role, npc.getId());
        FightObject fightObject = new TowerFightObject(monsterId, xinJun, layer, (short) npc.getIcon(), index, 0, role.getLevel());
        if (towerRankRecord != null) {
            fightObject.setIcon(towerRankRecord.getIcon());
            fightObject.setWeaponId(towerRankRecord.getWeaponIcon());
            fightObject.setSuitIcon(towerRankRecord.getSuitIcon());
        }

        FightMember fightMember = fightObject.asFightMember();
        if (towerRankRecord != null) {
            fightMember.setWeapon_icon(towerRankRecord.getWeaponIcon());
            fightMember.setSuit_icon(towerRankRecord.getSuitIcon());
            fightMember.setSuit_light_effect(towerRankRecord.getSuitLightEffect());
        }

        fightSide.getFightObjectList().add(fightObject);
        fightSide.getFightMemberList().add(fightMember);

    }

    private void setTowerPetToFightSide(FightSide fightSide, int index, int monsterId, Role role, int hostId) {
        String name = SMALL_XIN_JUN_NAMES.get(ThreadLocalRandom.current().nextInt(SMALL_XIN_JUN_NAMES.size()));
        int layer = role.getActivity().getRoleTowerData().getCurrentLayer();
        XinJun xinJun = SpringUtils.getBean(TowerTaskHandler.class).getXinjun(name);

        FightObject fightObject = new TowerFightObject(monsterId,xinJun,layer, (short) xinJun.getPetIcon(),index,hostId, role.getLevel());

        FightMember fightMember =  fightObject.asFightMember();
        fightSide.getFightMemberList().add(fightMember);
        fightSide.getFightObjectList().add(fightObject);
    }


}
